package com.code.test.first.hash;

/**
 * https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.md
 * <p>
 * 给定两个字符串 s 和 t ，编写一个函数来判断 t 是否是 s 的字母异位词。
 * <p>
 * 示例 1: 输入: s = "anagram", t = "nagaram" 输出: true
 * <p>
 * 示例 2: 输入: s = "rat", t = "car" 输出: false
 * <p>
 * 说明: 你可以假设字符串只包含小写字母。
 */
public class Code242 {

    public static void main(String[] args) {
        String s1 = "anagram";
        String s2 = "nagaram";

        System.out.println(isAnagram(s1, s2));

        s1 = "rat";
        s2 = "cat";

        System.out.println(isAnagram(s1, s2));

    }

    public static boolean isAnagram(String s1, String s2) {
        int[] map = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            map[s1.charAt(i) - 'a']++;
        }

        for (int i = 0; i < s2.length(); i++) {
            map[s2.charAt(i) - 'a']--;
        }

        for (int s : map) {
            if (s != 0) {
                return false;
            }
        }
        return true;
    }


}
